<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head th:replace="common/head::head">
</head>

<body>
<div class="layui-row">
    <button onclick="editUser('')" class="layui-btn layui-btn-sm">新增用户</button>
    <button id="refresh" class="layui-btn layui-btn-sm">刷新表格</button>
</div>
<table class="layui-table" id="tab_user" lay-filter="tab_user" style="margin: 0;"></table>

</body>

<script type="text/html" id="bar_projects">
    <a href="javascript:;" class="layui-btn  layui-btn-sm layui-btn-normal" lay-event="update">编辑</a>
    <a href="javascript:;" class="layui-btn  layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>

    {{# if(d.pwdErrorCount>0){ }}
    <a href="javascript:;" class="layui-btn  layui-btn-sm layui-btn-warm" lay-event="unlock">解锁</a>
    {{# } }}
</script>
<script type="text/html" id="manager_status">
    <div title="登录失败次数：{{d.pwdErrorCount}}">
        <input type="checkbox" id="" disabled name="status" {{# if(d.serverManager){ }}checked{{# } }}
               lay-skin="switch"
               lay-text="是|否">
    </div>
</script>
<div th:replace="common/sha1::sha1"></div>
<script type="text/javascript">
    var col = [
        {field: 'id', title: 'id'},
        {field: 'name', title: '昵称'},
        {field: 'email', title: '邮箱'},
        {field: 'parent', title: '创建人'},
        {
            field: 'modifyTime', title: '修改时间', templet: function (d) {
                return formateTime(d.modifyTime);
            }
        },
        {field: 'op', title: '操作', align: 'center', toolbar: '#bar_projects', fixed: 'right'}
    ];

    function loadSuccess() {

        tableRender({
            id: 'tab_user',
            elem: '#tab_user',
            url: './getUserList',
            cols: [col]
        });

        // '刷新表格'点击事件
        $('#refresh').on('click', function () {
            table.reload('tab_user', {});
        });

        // 表格工具条事件
        table.on('tool(tab_user)', function (obj) {
            var data = obj.data;
            var event = obj.event;
            if ('update' === event) {
                // 修改
                editUser(data.id);
            } else if ('delete' === event) {
                // 删除
                deleteApplication(data);
            } else if ('unlock' === event) {
                // 解锁
                unlockUser(data);
            }
        });


        // 删除用户
        function deleteApplication(data) {
            layer.confirm('确定删除用户 ' + data.id + '？', {
                title: '系统提示'
            }, function (index) {
                layer.close(index);
                loadingAjax({
                    url: './deleteUser',
                    data: {
                        id: data.id
                    },
                    success: function (data) {
                        layer.msg(data.msg);
                        if (200 === data.code) {
                            table.reload('tab_user', {});
                        }
                    }
                });
            });
        }
    }

    function editUser(id) {
        if ("[[${roleEmpty}]]" === "true") {
            layer.msg("请先创建角色");
            return;
        }
        layerOpen({
            type: 2,
            title: '编辑用户',
            content: './edit?id=' + id,
            area: ['80%', '80%'],
        });
    }

    function unlockUser(data) {
        layer.confirm('确定解锁用户 ' + data.id + '？', {
            title: '系统提示'
        }, function (index) {
            layer.close(index);
            loadingAjax({
                url: './unlock',
                data: {
                    id: data.id
                },
                success: function (data) {
                    layer.msg(data.msg);
                    if (200 == data.code) {
                        table.reload('tab_user', {});
                    }
                }
            });
        });
    }
</script>
</html>